package com.bcu.cn.onlineexamsystem.studentmanage.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

public class DeepSeekUtil {
    private static final String API_KEY = "sk-2ff0b4382658401e811d9b143e0b2130";
    private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";

    public static String askDeepSeek(String prompt) {
        try {
            JSONObject body = new JSONObject();
            body.put("model", "deepseek-chat");
            JSONArray messages = new JSONArray();
            JSONObject sys = new JSONObject();
            sys.put("role", "system");
            sys.put("content", "你是一名善于分析学生成绩并给出学习建议的学习顾问。");
            messages.add(sys);
            JSONObject user = new JSONObject();
            user.put("role", "user");
            user.put("content", prompt);
            messages.add(user);
            body.put("messages", messages);

            String resp = HttpRequest.post(API_URL)
                .header("Authorization", "Bearer " + API_KEY)
                .header("Content-Type", "application/json")
                .body(body.toString())
                .execute().body();
            JSONObject json = new JSONObject(resp);
            return json.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getStr("content");
        } catch (Exception e) {
            return "AI服务暂时不可用";
        }
    }
} 